const express = require('express')
const fs = require('fs')
const zlib = require('zlib')
let path = require('path')
const response = require('../../utils/response')
const Task = require('../../models/task')

const archiver = require('archiver');

// 创建一个空的压缩包
const gzib = zlib.createGzip()

//图片上传处理模块包
const multer = require('multer')
//图片上传OSS处理模块包
// const MAO = require('multer-aliyun-oss');

const router = express.Router()


function add0(m) {
    return m < 10 ? '0' + m : m
}
var time = new Date();
var y = time.getFullYear();
var m = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
m = add0(m)
d = add0(d)
h = add0(h)

//配置地址
const upload = multer({
    dest: 'uploads/' + y + '/' + m + '/' + d + '/'
})

//上传OSS配置
// const upload = multer({
//     storage: MAO({
//         config: {
//             region: 'oss-cn-shenzhen',
//             accessKeyId: 'LTAI5tLBxQCbAVYHAjrRmBzz',
//             accessKeySecret: 'wUqVKZHAWdeEMp3fCN2rEU8uQcGoaP',
//             bucket: 'task-uploads'
//         }
//     })
// })



router.post('/', upload.array('files', 9), async (req, res) => {
    const files = req.files
    if (files.length == 0) {
        return response(res, 401, '没有选择文件', files)
    }
    for (var i in files) {
        let oldname = req.files[i].path //获取path
        let newname = req.files[i].path + '-' + req.files[i].originalname
        fs.renameSync(oldname, newname)
    }
    response(res, 200, '上传成功', files)

})

module.exports = router